#!/usr/bin/env python
# -*- coding:utf-8 -*-


import urllib2
from bs4 import BeautifulSoup
import re
from mylog import MyLog
from save2excel import SaveBallData


class DoubleColorBallItem(object):
    # date = None
    order = None
    red1 = None
    red2 = None
    red3 = None
    red4 = None
    red5 = None
    red6 = None
    blue = None


class GetDoubleColorBallNumber(object):
    def __init__(self):
        self.urls = []
        self.log = MyLog()
        self.urls = self.get_urls()
        self.items = []
        self.items = self.spider(self.urls)
        self.pipelines(self.items)
        SaveBallData(self.items)


    def get_urls(self):
        # URL = 'http://m.zhcw.com/kaijiang/'
        URL = 'http://localhost:8888'
        urls = []
        urls.append(URL)

        self.log.info('Get Urls Success')

        return urls


    def get_response_content(self, url):
        try:
            response = urllib2.urlopen(url)
        except:
            self.log.error("Url:%s Open Error" % url)
        else:
            self.log.info('Url:%s Open Success' % url)
            return response.read()


    def spider(self, urls):
        items = []
        for url in urls:
            html_content = self.get_response_content(url)
            # print html_content
            soup = BeautifulSoup(html_content, 'lxml')
            tags = soup.find_all('section', attrs={'class': 'kj_num kj_num_no'})

            # print tags

            for tag in tags:
                item = DoubleColorBallItem()
                # date = tag.ul.p.span.get_text()
                order = tag.ul.p.get_text()
                balls = tag.find_all('li', attrs={'class': 'red_ball'})
                red1 = balls[0].get_text()
                red2 = balls[1].get_text()
                red3 = balls[2].get_text()
                red4 = balls[3].get_text()
                red5 = balls[4].get_text()
                red6 = balls[5].get_text()
                blue = tag.find('li', attrs={'class': 'blue_ball'}).get_text()

                print '----------------print------------------'
                # print date
                print order
                print red1, red2, red3, red4, red5, red6, blue

                # item.date = date
                item.order = order
                item.red1 = red1
                item.red2 = red2
                item.red3 = red3
                item.red4 = red4
                item.red5 = red5
                item.red6 = red6
                item.blue = blue

                items.append(item)

        self.log.info('Get Items Success')
        return items


    def pipelines(self, items):
        filename = 'lottery.txt'
        with open(filename, 'a') as fp:
            for item in items:
                # fp.write(item.date.encode('utf8') + '\r')
                fp.write(item.order.encode('utf8') + '\r')
                fp.write(item.red1.encode('utf8') + ' ')
                fp.write(item.red2.encode('utf8') + ' ')
                fp.write(item.red3.encode('utf8') + ' ')
                fp.write(item.red4.encode('utf8') + ' ')
                fp.write(item.red5.encode('utf8') + ' ')
                fp.write(item.red6.encode('utf8') + ' ')
                fp.write(item.blue.encode('utf8') + '\n')

        self.log.info('Write To File Success')


if __name__ == '__main__':
    GDCBN = GetDoubleColorBallNumber()
